package net.zjitc.servlet.goods;

import net.zjitc.dao.GoodsDao;
import net.zjitc.dao.impl.GoodsDaoImpl;
import net.zjitc.domain.Goods;
import net.zjitc.service.impl.GoodsServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "GoodsListServlet",urlPatterns = "/goodsList")
public class GoodsListServlet extends HttpServlet {
    GoodsDao goodsDao = new GoodsDaoImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        HttpSession session = request.getSession();
        Object user=session.getAttribute("user");
        if (user==null) {
            RequestDispatcher dispatcher=request.getRequestDispatcher("/login/login.jsp");
            dispatcher.forward(request,response);
        } else {
            List<Goods> list = goodsDao.findAll();
            request.setAttribute("li", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/goods/goodslist.jsp");
            dispatcher.forward(request, response);
        }
    }
}
